package com.hais1992.oneday.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.hais1992.oneday.R;
import com.hais1992.oneday.common.Config;
import com.hais1992.oneday.entity.User;
import com.hais1992.oneday.tools.GetHttpData;
import com.hais1992.oneday.tools.GetHttpData.GetCallback;

/**
 * 用户登录
 * 
 * @author Hello_海生
 * @date 2014年5月26日
 */
public class LoginActivity extends Activity {
	private ImageView userImg; // 右邊的 個人中心
	private ImageView retuenImg; // 左邊的 返回按钮
	private TextView titleTv; // 标题栏

	private EditText emailEt; // 登陆邮箱
	private EditText passwordEt; // 登陆密码
	private Button buttonBt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_login);
		
		userImg = (ImageView) findViewById(R.id.rightImg);
		retuenImg = (ImageView) findViewById(R.id.leftImg);
		titleTv = (TextView) findViewById(R.id.titleTv);
		emailEt = (EditText) findViewById(R.id.emailEt);
		passwordEt = (EditText) findViewById(R.id.passwordEt);
		buttonBt = (Button) findViewById(R.id.loginBt);

		
		buttonBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("用户登陆："+emailEt.getText());
				
				Map<String, String> map = new HashMap<String, String>();
				map.put("email", emailEt.getText()+"");
				map.put("key", Config.SERVER_KEY);
				
				new GetHttpData(Config.SERVER_URL+Config.GET_USER_URL, "POST", map, new GetCallback() {
					@Override
					public void onSuccess(String result) {
						JSONObject jsonObject = JSONObject.parseObject(result);
						if(jsonObject.getString("static").equals("error")){
							Toast.makeText(LoginActivity.this, "登陆失败！该账号不存在！", Toast.LENGTH_SHORT).show();
						}else{
							User user = jsonObject.getObject("user", User.class);
							if(passwordEt.getText().toString().equals(user.getPassword().toString())){
								Toast.makeText(LoginActivity.this, "登陆成功！", Toast.LENGTH_SHORT).show();
								Config.cacheToken(LoginActivity.this, user.getEmail());
								LoginActivity.this.finish();
							}else{
								Toast.makeText(LoginActivity.this, "登陆失败，密码错误！", Toast.LENGTH_SHORT).show();
							}
						}
						
					}
				});
			}
		});
		
		
		
		
		
		
		titleTv.setText("登陆 OneDay");			//设置标题
		userImg.setVisibility(View.INVISIBLE);	// 設置 隐藏 個人中心 菜單

		// 设置返回键按钮
		retuenImg.setImageDrawable(getResources().getDrawable(R.drawable.left));
		retuenImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LoginActivity.this.finish();
			}
		});

	}
}
